<template>
  <div class="category">
    <!-- 金刚位组件 -->
    <div class="category__top" @scroll="onScroll">
      <div class="category__top__list" :style="{ width: listW + 'px' }">
        <div
          v-for="(item, index) in data"
          :key="index"
          class="category__top__list__item"
        >
          <van-image
            class="category__top__list__item__img"
            :src="item.imageUrl"
          />
          <div class="category__top__list__item__text">
            {{ item.name }}
          </div>
        </div>
      </div>
    </div>
    <div class="category__bottom">
      <div class="category__bottom__bg">
        <div class="category__bottom__bg__progress" :style="{left: scrollW + 'px'}" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      data: [],
      // 金刚位一行的宽度
      listW: 0,
      // 横向滚动条可滚动区域
      progressScrollW: 0,
      // 金刚位可滚动区域
      categoryScrollW: 0,
      scrollW: 0 // 进度条偏移量
    }
  },
  watch: {
    '$store.state.homeList.category': {
      // 初次渲染立即获得一次数据
      immediate: true,
      handler(val) {
        this.data = val
        this.calc()
      }
    }
  },
  methods: {
    calc() {
      this.$nextTick(() => {
        // 获取换行位置（1/2的list数量（向上取整））
        const lineCount = Math.ceil(this.data.length / 2)
        // 获取单个item元素（计算item宽度，边距）
        const item = document.getElementsByClassName(
          'category__top__list__item'
        )[0]
        const itemW = item.getBoundingClientRect().width
        const itemStyle = window.getComputedStyle(item)
        const marginLeft = +itemStyle.marginLeft.replace('px', '')
        const marginRight = +itemStyle.marginRight.replace('px', '')

        // 计算一行宽度
        this.listW = (itemW + marginLeft + marginRight) * lineCount

        // 获取滚动条背景信息
        const bg = document.getElementsByClassName('category__bottom__bg')[0]
        // 获取滚动条信息
        const progress = document.getElementsByClassName('category__bottom__bg__progress')[0]
        // 滚动条可滚动区域的宽度
        this.progressScrollW = bg.getBoundingClientRect().width - progress.getBoundingClientRect().width
        // 金刚可滚动区域
        const category = document.getElementsByClassName('category__top')[0]
        this.categoryScrollW = this.listW - category.getBoundingClientRect().width
      })
    },
    onScroll(e) {
      const { scrollLeft } = e.target
      let scrollPercent = scrollLeft / this.categoryScrollW
      if (scrollPercent > 1) {
        scrollPercent = 1
      } else if (scrollPercent < 0) {
        scrollPercent = 0
      }
      this.scrollW = scrollPercent * this.progressScrollW
    }
  }
}
</script>

<style lang="less">
.category {
  margin: 0 18px;
  background: #ffffff;
  border-radius: 24px;
  // overflow: hidden;
  &__top {
    width: 100%;
    height: 265px;
    overflow-y: hidden;

    overflow-x: auto;
    &::-webkit-scrollbar {
      display: none;
    }
    &__list {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      flex-direction: row;
      flex-wrap: wrap;
      // overflow-x: auto;

      &__item {
        width: 104px;
        height: 125px;
        margin: 9px 16px 0 16px;
        display: flex;
        flex-direction: column;
        // justify-content: center;
        align-items: center;
        &__img {
          width: 88px;
          height: 88px;
        }
        &__text {
          width: 104px;
          height: 37px;
          font-size: 26px;
          color: #717171;
          text-align: center;
        }
      }
    }
  }
  &__bottom {
    height: 29px;
    margin-top: 10px;
    padding-bottom: 13px;
    display: flex;
    align-items: center;
    justify-content: center;
    &__bg {
      position: relative;
      width: 86px;
      height: 6px;
      background: #d4d8dd;
      border-radius: 2px;
      &__progress {
        position: absolute;
        left: 0;
        top: 0;
        width: 43px;
        height: 6px;
        background: #ff6d29;
        border-radius: 2px;
      }
    }
  }
}
</style>
